package com.meterhuang.thread.zeroevenodd;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author meterhuang
 * @date 2021/09/09 11:54
 */
@Slf4j
public class ZeroEvenOddPrint {

	public static void main(String[] args) {
		//ZeroEvenOddSemaphore semaphore = new ZeroEvenOddSemaphore(20);
		ZeroEvenOddReentrant semaphore = new ZeroEvenOddReentrant(20);
		ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
				3,
				3,
				10000,
				TimeUnit.SECONDS,
				new ArrayBlockingQueue<>(3),
				Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.DiscardPolicy()
		);

		threadPool.submit(() -> {
			try {
				semaphore.zero(value -> log.info("{}", value));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		threadPool.submit(() -> {
			try {
				semaphore.even(value -> log.info("{}", value));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		threadPool.submit(() -> {
			try {
				semaphore.odd(value -> log.info("{}", value));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}

}
